package com.jinxin.platform.cdfaceacs.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdfaceacs.bean.CdfaceacsDevice;
import com.jinxin.platform.cdfaceacs.common.Constant;
import com.jinxin.platform.cdfaceacs.mapper.CdfaceacsDeviceMapper;
import com.jinxin.platform.cdfaceacs.rpc.SysUserRpc;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsDeviceService;
import com.jinxin.platform.cdfaceacs.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-05-20
 * @Author: Lingnan
 **/
@Service
public class FaceAcsDeviceServiceImpl implements IFaceAcsDeviceService {
    @Autowired
    private CdfaceacsDeviceMapper deviceMapper;
    @Autowired
    private SysUserRpc sysUserRpc;

    @Override
    public List<CdfaceacsDevice> listPage(HashMap param) {
        return deviceMapper.listPage(param);
    }

    @Override
    public List<CdfaceacsDevice> list(HashMap param) {
        return deviceMapper.list(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return deviceMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("设备Id不存在");
        }
        int result = deviceMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdfaceacsDevice deviceInfo) {
        if (deviceInfo == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = deviceInfo.getId();
        int count = 0;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            deviceInfo.setCreateTime(LocalDateTime.now());
            deviceInfo.setId(StringUtil.getUuid());
            count = deviceMapper.insert(deviceInfo);
            msg = "新增成功";
        }else {
            // 更改密码
            CdfaceacsDevice device = deviceMapper.selectByKey(id);
            String oldPassword = device != null && !StringUtils.isEmpty(device.getPassword()) ? device.getPassword() : Constant.DEVICE_INIT_PASSWORD;
            String newPassword = deviceInfo.getPassword();
            if(!StringUtils.isEmpty(newPassword)){
                JsonResult result = sysUserRpc.interfacePasswordSetting(oldPassword, newPassword, device.getSerialNum(), device.getProductCode());
                if(result.getSuccess()){
                    count = deviceMapper.updateByKey(deviceInfo);
                    msg = "更新成功";
                }else {
                    return JsonResult.error("更新密码失败，操作失败");
                }
            }else {
                count = deviceMapper.updateByKey(deviceInfo);
            }
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public CdfaceacsDevice selectOne(HashMap param) {
        return deviceMapper.selectOne(param);
    }
}
